package com.sjy.pickphotos.pickphotos.luban;

import android.content.Context;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.os.Handler.Callback;
import android.support.annotation.Nullable;
import android.support.annotation.UiThread;
import android.support.annotation.WorkerThread;
import android.text.TextUtils;
import android.util.Log;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class Luban implements Callback {
    private String mTargetDir;
    private List<String> mPaths;
    private int mLeastCompressSize;
    private OnCompressListener mCompressListener;
    private Handler mHandler;

    private Luban(Builder builder) {
        this.mPaths = builder.mPaths;
        this.mTargetDir = builder.mTargetDir;
        this.mCompressListener = builder.mCompressListener;
        this.mLeastCompressSize = builder.mLeastCompressSize;
        this.mHandler = new Handler(Looper.getMainLooper(), this);
    }

    public static Builder with(Context context) {
        return new Builder(context);
    }

    private File getImageCacheFile(Context context, String suffix) {
        if (TextUtils.isEmpty(this.mTargetDir)) {
            this.mTargetDir = this.getImageCacheDir(context).getAbsolutePath();
        }

        String cacheBuilder = this.mTargetDir + "/" + System.currentTimeMillis() + (int)(Math.random() * 1000.0D) + (TextUtils.isEmpty(suffix) ? ".jpg" : suffix);
        return new File(cacheBuilder);
    }

    @Nullable
    private File getImageCacheDir(Context context) {
        return this.getImageCacheDir(context, "luban_disk_cache");
    }

    @Nullable
    private File getImageCacheDir(Context context, String cacheName) {
        File cacheDir = context.getExternalCacheDir();
        if (cacheDir == null) {
            if (Log.isLoggable("Luban", 6)) {
                Log.e("Luban", "default disk cache dir is null");
            }

            return null;
        } else {
            File result = new File(cacheDir, cacheName);
            return result.mkdirs() || result.exists() && result.isDirectory() ? result : null;
        }
    }

    @UiThread
    private void launch(final Context context) {
        if (this.mPaths == null || this.mPaths.size() == 0 && this.mCompressListener != null) {
            this.mCompressListener.onError(new NullPointerException("image file cannot be null"));
        }

        for(Iterator iterator = this.mPaths.iterator(); iterator.hasNext(); iterator.remove()) {
            final String path = (String)iterator.next();
            if (Checker.isImage(path)) {
                AsyncTask.SERIAL_EXECUTOR.execute(new Runnable() {
                    public void run() {
                        try {
                            Luban.this.mHandler.sendMessage(Luban.this.mHandler.obtainMessage(1));
                            File result = Checker.isNeedCompress(Luban.this.mLeastCompressSize, path) ? (new Engine(path, Luban.this.getImageCacheFile(context, Checker.checkSuffix(path)))).compress() : new File(path);
                            Luban.this.mHandler.sendMessage(Luban.this.mHandler.obtainMessage(0, result));
                        } catch (IOException var2) {
                            Luban.this.mHandler.sendMessage(Luban.this.mHandler.obtainMessage(2, var2));
                        }

                    }
                });
            } else {
                Log.e("Luban", "can not read the path : " + path);
            }
        }

    }

    @WorkerThread
    private File get(String path, Context context) throws IOException {
        return (new Engine(path, this.getImageCacheFile(context, Checker.checkSuffix(path)))).compress();
    }

    @WorkerThread
    private List<File> get(Context context) throws IOException {
        List<File> results = new ArrayList();

        for(Iterator iterator = this.mPaths.iterator(); iterator.hasNext(); iterator.remove()) {
            String path = (String)iterator.next();
            if (Checker.isImage(path)) {
                results.add((new Engine(path, this.getImageCacheFile(context, Checker.checkSuffix(path)))).compress());
            }
        }

        return results;
    }

    public boolean handleMessage(Message msg) {
        if (this.mCompressListener == null) {
            return false;
        } else {
            switch(msg.what) {
            case 0:
                this.mCompressListener.onSuccess((File)msg.obj);
                break;
            case 1:
                this.mCompressListener.onStart();
                break;
            case 2:
                this.mCompressListener.onError((Throwable)msg.obj);
            }

            return false;
        }
    }

    public static class Builder {
        private Context context;
        private String mTargetDir;
        private List<String> mPaths;
        private int mLeastCompressSize = 100;
        private OnCompressListener mCompressListener;

        Builder(Context context) {
            this.context = context;
            this.mPaths = new ArrayList();
        }

        private Luban build() {
            return new Luban(this);
        }

        public Builder load(File file) {
            this.mPaths.add(file.getAbsolutePath());
            return this;
        }

        public Builder load(String string) {
            this.mPaths.add(string);
            return this;
        }

        public Builder load(List<String> list) {
            this.mPaths.addAll(list);
            return this;
        }

        public Builder putGear(int gear) {
            return this;
        }

        public Builder setCompressListener(OnCompressListener listener) {
            this.mCompressListener = listener;
            return this;
        }

        public Builder setTargetDir(String targetDir) {
            this.mTargetDir = targetDir;
            return this;
        }

        public Builder ignoreBy(int size) {
            this.mLeastCompressSize = size;
            return this;
        }

        public void launch() {
            this.build().launch(this.context);
        }

        public File get(String path) throws IOException {
            return this.build().get(path, this.context);
        }

        public List<File> get() throws IOException {
            return this.build().get(this.context);
        }
    }
}